﻿<%@ WebHandler Language="C#" Class="getlist" %>

using System;
using System.Web;
using System.Web.Script.Serialization;
using System.Data;
using System.Xml;

public class getlist : IHttpHandler {
    
    
    
    public void ProcessRequest (HttpContext context) {
        string action = context.Request.Params["act"];
        JavaScriptSerializer js = new JavaScriptSerializer();
        object json = new object();
        if (action == "getcount")
        {
            string countjson = "{\"count\":\"" + GetRecordCount(1) + "\"}";
            json = js.Serialize(countjson);
            context.Response.ContentType = "application/json";
            context.Response.Write(json);
            context.Response.End();
        }
        else if (action == "getlist")
        {
            int currentindex = int.Parse(context.Request.Params["page"]);
            DataSet ds = GetList(currentindex);
            if (ds != null)
            {
                ds.Tables[0].Columns.Remove("Content");
                for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                {
                    ds.Tables[0].Columns[0].ColumnMapping = MappingType.Attribute;
                }
                ds.DataSetName = "List";
                string xml = ds.GetXml();
                xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + xml;
                context.Response.ContentType = "text/xml";
                context.Response.Write(xml);
                context.Response.End();
            }
        }
        
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    private DataSet GetList(int currentindex)
    {
        int num;
        if (currentindex < 1)
            currentindex = 1;
        DataSet ds = new Cstudy.BLL.resources().SelectResources(currentindex, 20, out num);
        int RecordCount = num;
        if (ds != null)
        {
            return ds;
        }
        return null;
    }

    private string GetRecordCount(int currentindex)
    {
        int num;
        DataSet ds = new Cstudy.BLL.resources().SelectResources(currentindex, 20, out num);
        int RecordCount = num;
        return RecordCount.ToString();
    }

    /// <summary>      
    /// dataTable转换成Json格式      
    /// </summary>      
    /// <param name="dt"></param>      
    /// <returns></returns>      
    public static string ToJson(DataTable dt)
    {
        System.Text.StringBuilder jsonBuilder = new System.Text.StringBuilder();
        jsonBuilder.Append("{\"");
        jsonBuilder.Append(dt.TableName.ToString());
        jsonBuilder.Append("\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            jsonBuilder.Append("{");
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                jsonBuilder.Append("\"");
                jsonBuilder.Append(dt.Columns[j].ColumnName);
                jsonBuilder.Append("\":\"");
                jsonBuilder.Append(HttpUtility.HtmlEncode(dt.Rows[i][j].ToString()));
                jsonBuilder.Append("\",");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("},");
        }
        jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
        jsonBuilder.Append("]");
        jsonBuilder.Append("}");
        return jsonBuilder.ToString();
    }   
}